/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package BusinessLogic;

import DTO.Zone;
import DataAccess.DataUtility;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.Vector;

/**
 *
 * @author Tran anh
 * @version 1.0
 */
public class ZoneManagement {

    /**
     * Create a vector to contain list zones
     */
    private Vector<Zone> listZone;
    
    public ZoneManagement() {
    }

    /**
     * load list zone from database
     * @return list zone
     */
    public Vector<Zone> loadZone() {
        try {
            listZone = new Vector<Zone>();

            ResultSet rs = DataUtility.excuteQuery("Select * from Zone where status = 1");
            while (rs.next()) {
                Zone zone = new Zone();
                listZone.add(zone);

                zone.setIDzone(rs.getString("idZone"));
                zone.setMoney(rs.getFloat("money"));
                zone.setName(rs.getString("name"));
                zone.setNumberBooth(rs.getInt("numberBooth"));
                zone.setSize(rs.getString("Size"));
            }
        } catch (Exception e) {      
            e.printStackTrace();
            return null;
        }
        return listZone;
    }

    /**
     * Return a Zone folow by it's index in array
     * @param index is specify location of it in array
     * @return a object Zone
     */
    public Zone getByIndex(int index){
        if(listZone.size() > 0){
            return listZone.get(index);
        }
        return null;
    }

    /**
     * Generate new ID of Zone and not same other Id has in database
     * @return String new ID
     */
    public String createID(){
        String id = "AZ";
        int amount = 0;
        try {
            ResultSet rs = DataUtility.excuteQuery("select count(*) from Zone");
            rs.next();
            amount = rs.getInt(1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String number = ""+ (amount + 1);
        if(number.length() == 1)
            id = id + "000";
        else if(number.length() == 2)
            id = id + "00";
        else if(number.length() == 3)
            id = id + "0";
        id = id + number;
        return id;
    }

    /**
     * Get information of object Zone put into HashMap.
     * Then call method executeProcedure to insert object into database
     * @param zone  is new Object to get information pass into database
     */
    public void insert(Zone zone) {
        HashMap fiels = new HashMap();
        fiels.put("idZone", zone.getIDzone());
        fiels.put("name", zone.getName());
        fiels.put("numberBooth", zone.getNumberBooth());
        fiels.put("Money", zone.getMoney());
        fiels.put("size", zone.getSize());

        DataUtility.executeProcedure("insertZone", fiels);
    }

    /**
     * Get information of object Zone put into HashMap.
     * Then call method executeProcedure to update object into database
     * @param zone  is new Object to get information pass into database
     */
    public void update(Zone zone) {
        HashMap fiels = new HashMap();
        fiels.put("idZone", zone.getIDzone());
        fiels.put("name", zone.getName());
        fiels.put("numberBooth", zone.getNumberBooth());
        fiels.put("Money", zone.getMoney());
        fiels.put("size", zone.getSize());

        DataUtility.executeProcedure("updateZone", fiels);
    }
}
